import Vue from 'vue'
import axios from 'axios';
import NProgress from 'nprogress'//加载进度条
import 'nprogress/nprogress.css'

Vue.prototype.$url = "http://km.cdn001-baidu.com:9070"

let baseurl = Vue.prototype.$url
//请求拦截
axios.interceptors.request.use(function (config) {
  NProgress.start()
  let token = sessionStorage.getItem('token')
  if (token) {
    config.headers.Authorization = token
  }
  return config
}, function (error) {
  return Promise.reject(error)
})
//响应拦截  NProgress.done()
axios.interceptors.response.use(config => {
  NProgress.done()
  return config
}, function (error) {
  if (error) {
    sessionStorage.removeItem('token')
    this.$router.push('/login')
  }
  return Promise.reject(error)
})
//get方法
export function myGet(url, params = {}) {
  let _url = baseurl + url
  return new Promise((resolve, reject) => {
    axios.get(_url, {
      params: params
    })
      .then(response => {
        resolve(response);
      })
      .catch(err => {
        reject(err)
      })
  })
}
//post方法
export function myPost(url, data = {}) {
  let _url = baseurl + url
  return new Promise((resolve, reject) => {
    axios.post(_url, data)
      .then(response => {
        resolve(response);
      }, err => {
        reject(err)
      })
  })
}
//delete方法
export function myDelete(url, data = {}) {
  let _url = baseurl + url
  return new Promise((resolve, reject) => {
    axios.delete(_url, data)
      .then(response => {
        resolve(response);
      }, err => {
        reject(err)
      })
  })
}
